<?php
namespace app\common\model;
use think\Model;

class Category extends Model{
    // 读取器
    protected function getCreateTimeAttr($data){
        return date('Y-m-d', $data);
    }
    
    /**
     * 分类添加函数
     */
    public function add($data){
        $data['status'] = 1;
        $data['create_time'] = time();
        return $this->save($data);
    }
    
    /**
     * 获取顶级分类
     */
    public function getNormalFirstCategory(){
        $data = [
            'status' => 1,
            'parent_id' => 0,
        ];
        
        $order = [
            'id' => 'desc',
        ];
        
        return $this->where($data)->order($order)->select();
    }
    
    /**
     * 获取分类信息
     */
    public function getCategories($parentId = 0){
        $data = [
            'parent_id' => $parentId,
            'status' => ['neq',-1],
        ];
        $order = [
            'listoder' => 'desc',
            'id' => 'desc',
        ];
        $result = $this->where($data)->order($order)->select();
        
        return $result;
    }


    /**
     * 统计分类个数
     */
    public function countCategory(){
    $count = $this->where('status', 1)->count();
    return $count;
    }
}
